// cn/iocoder/yudao/module/gold/dal/mysql/record/GoldMaterialRecordMapper.java
package cn.iocoder.yudao.module.system.dal.mysql.warehouse;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.warehouse.vo.GoldMaterialRecordPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.warehouse.GoldMaterialRecordDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface GoldMaterialRecordMapper extends BaseMapperX<GoldMaterialRecordDO> {

    default PageResult<GoldMaterialRecordDO> selectPage(GoldMaterialRecordPageReqVO reqVO) {
        LambdaQueryWrapperX<GoldMaterialRecordDO> wrapper = new LambdaQueryWrapperX<GoldMaterialRecordDO>()
                .eqIfPresent(GoldMaterialRecordDO::getWarehouseId, reqVO.getWarehouseId())
                .eqIfPresent(GoldMaterialRecordDO::getOperationType, reqVO.getOperationType())
                .eqIfPresent(GoldMaterialRecordDO::getOperationSubtype, reqVO.getOperationSubtype());

        // 1) 优先使用数组 createTime
        if (reqVO.getCreateTime() != null && reqVO.getCreateTime().length == 2
                && reqVO.getCreateTime()[0] != null && reqVO.getCreateTime()[1] != null) {
            wrapper.between(GoldMaterialRecordDO::getCreateTime,
                    reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
            // 2) 其次使用 List createTimeList
        } else if (reqVO.getCreateTimeList() != null && reqVO.getCreateTimeList().size() == 2
                && reqVO.getCreateTimeList().get(0) != null && reqVO.getCreateTimeList().get(1) != null) {
            wrapper.between(GoldMaterialRecordDO::getCreateTime,
                    reqVO.getCreateTimeList().get(0), reqVO.getCreateTimeList().get(1));
            // 3) 兜底：startTime / endTime 任意存在
        } else {
            if (reqVO.getStartTime() != null) {
                wrapper.ge(GoldMaterialRecordDO::getCreateTime, reqVO.getStartTime());
            }
            if (reqVO.getEndTime() != null) {
                wrapper.le(GoldMaterialRecordDO::getCreateTime, reqVO.getEndTime());
            }
        }

        return selectPage(reqVO, wrapper.orderByDesc(GoldMaterialRecordDO::getId));
        //return new PageResult<>(page.getRecords(), page.getTotal());
    }

    /**
     * 查询今日入料总量
     */
    default BigDecimal selectTodayInTotal(Long warehouseId) {
        LocalDateTime today = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime tomorrow = today.plusDays(1);

        LambdaQueryWrapper<GoldMaterialRecordDO> wrapper = new LambdaQueryWrapperX<GoldMaterialRecordDO>()
                .eqIfPresent(GoldMaterialRecordDO::getWarehouseId, warehouseId)
                .eq(GoldMaterialRecordDO::getOperationType, 1) // 入料
                .ge(GoldMaterialRecordDO::getCreateTime, today)
                .lt(GoldMaterialRecordDO::getCreateTime, tomorrow)
                .eq(GoldMaterialRecordDO::getDeleted, false);

        List<GoldMaterialRecordDO> records = selectList(wrapper);
        return records.stream()
                .map(GoldMaterialRecordDO::getOperationWeight)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    /**
     * 查询今日出料总量
     */
    default BigDecimal selectTodayOutTotal(Long warehouseId) {
        LocalDateTime today = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime tomorrow = today.plusDays(1);

        LambdaQueryWrapper<GoldMaterialRecordDO> wrapper = new LambdaQueryWrapperX<GoldMaterialRecordDO>()
                .eqIfPresent(GoldMaterialRecordDO::getWarehouseId, warehouseId)
                .eq(GoldMaterialRecordDO::getOperationType, 2) // 出料
                .ge(GoldMaterialRecordDO::getCreateTime, today)
                .lt(GoldMaterialRecordDO::getCreateTime, tomorrow)
                .eq(GoldMaterialRecordDO::getDeleted, false);

        List<GoldMaterialRecordDO> records = selectList(wrapper);
        return records.stream()
                .map(GoldMaterialRecordDO::getOperationWeight)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

}